package com.example.day1_dialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private int progress = 0;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //普通对话框
        findViewById(R.id.bt_normal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                //TODO 1:构建者
//                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//                //TODO 2:设置属性
//                builder.setIcon(R.drawable.ic_launcher_background);
//                builder.setTitle("标题");
//                builder.setMessage("内容");
//                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
//                    }
//                });
//                //TODO 3:创建对话框
//                AlertDialog alertDialog = builder.create();
//                //TODO 4:显示对话框
//                alertDialog.show();


                //链式编程
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.ic_launcher_background)
                        .setTitle("标题")//设置标题
                        .setMessage("内容")//设置内容
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {//设置确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {//设置取消按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "ccancel", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();//显示对话框

            }
        });
    //单选对话框
        findViewById(R.id.bt_single).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] strings = new String[]{"男人","女人","不详"};//单选列表
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.ic_launcher_background)
                        .setTitle("标题")
                       // .setMessage("2")//注意内容不要设置
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        //参数一 数组列表 参数二 默认选中下标 参数三 点击的监听
                        .setSingleChoiceItems(strings, 1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, ""+strings[which], Toast.LENGTH_SHORT).show();

                            }
                        })
                        .show();
            }
        });
//多选对话框
        findViewById(R.id.bt_multi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] strings = new String[]{"如花","凤姐","小赵"};
                boolean[] booleans = new boolean[]{true,true,true};



                new AlertDialog.Builder(MainActivity.this)//上下文
                        .setIcon(R.drawable.ic_launcher_background)
                        .setTitle("标题")
                        // .setMessage("2")//注意内容不要设置
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        //参数一 列表 参数二 默认是否选中列表必须是boolean  参数三 监听
                        .setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                Toast.makeText(MainActivity.this, ""+strings[which]+"变成"+isChecked, Toast.LENGTH_SHORT).show();

                            }
                        })
                        .show();

            }
        });
        //水平进度条
        findViewById(R.id.bt_progress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置样式 水平
                progressDialog.setMax(100);//最大值
                progressDialog.setMessage("------");
                progressDialog.show();//显示

                //定时器
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if(progress == 100){
                            progressDialog.dismiss();//消失
                            timer.cancel();//取消定时器
                            progress = 0;
                        }
                        progressDialog.setProgress(progress+=20);
                    }
                },0,1000);


            }
        });

        //圆圈进度条
        findViewById(R.id.bt_ciecle_progress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置样式 圆圈
                progressDialog.setMax(100);//最大值
                progressDialog.setMessage("------");
                progressDialog.show();//显示

                //定时器
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if(progress == 100){
                            progressDialog.dismiss();//消失
                            timer.cancel();//取消定时器
                            progress = 0;
                        }
                        progressDialog.setProgress(progress+=20);
                    }
                },0,1000);


            }
        });
        //日期选择对话框
        findViewById(R.id.bt_calender).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar calendar= Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                final int day = calendar.get(Calendar.DAY_OF_MONTH);

                //参数一 上下文 参数二 监听 参数三 四 五 默认的年月日
                DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        Toast.makeText(MainActivity.this, "你选择了"+year+"年"+(month+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();

                    }
                }, year, month, day);
                datePickerDialog.show();
            }
        });
        //时间选择对话框
        findViewById(R.id.bt_time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar calendar = Calendar.getInstance();
                final int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);

                TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(MainActivity.this, "你选择了"+hourOfDay+"："+minute, Toast.LENGTH_SHORT).show();

                    }
                }, hour, minute, false);
                timePickerDialog.show();
            }
        });
    //自定义对话框
        findViewById(R.id.bt_customer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.customer_dialog, null);
                view.findViewById(R.id.iv).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
                        alertDialog.dismiss();//关闭对话框
                    }
                });

                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                        .setView(view);
                alertDialog = builder.create();//创建对话框
                alertDialog.show();
            }
        });


        findViewById(R.id.bt_customer2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new YaoDialog(MainActivity.this)
                        .setYesStr("是")
                        .setNoStr("不")
                        .setTitleStr("11111")
                        .setMessageStr("222222")
                        .setYesListener(new YaoDialog.YaoListener() {
                            @Override
                            public void onClick() {
                                Toast.makeText(MainActivity.this, "11111", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNoListener(new YaoDialog.YaoListener() {
                            @Override
                            public void onClick() {
                                Toast.makeText(MainActivity.this, "33333", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();

            }
        });

        findViewById(R.id.bt_pic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PicDialog(MainActivity.this)
                        .setTitle("sjsjskkskd")
                        .setUrl("https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2858426577,4189650377&fm=26&gp=0.jpg")
                        .setMyLongListener(new PicDialog.MyLongListener() {
                            @Override
                            public void onLongClick() {
                                Toast.makeText(MainActivity.this, "4444", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });








    }
}
